﻿var pub_wx_hide_menu = false;
var pub_page_init_data = {};


$(function ()
{
    //分享
    func_init_share(func_init_shareData());

    //page api
    console.log(typeof (pub_page_name));
    if (typeof (pub_page_name) != "undefined")
    {
        func_page_init();
    }
});


function func_page_init()
{
    var url = 'api';

    var __data =
        {
            __d: (new Date()).valueOf()
        };

    if (typeof (pub_page_init_data) != "undefined")
    {
        __data = $.extend({}, pub_page_init_data, __data);
    }

    func_ajax({
        url: url,
        data: __data,
        //////////////////////////////////////////////////
        success: function (data, textStatus, jqXHR)
        {
            setTimeout(function ()
            {
                func_page_init__success(data, textStatus, jqXHR);
            }, 100);
        }
    });
}

function func_page_init__success(data, textStatus, jqXHR)
{
    console.log(data);
    console.log(textStatus);
}


//////////////////////////////
///初始化分享参数
function func_init_share(shareData)
{
    wx.ready(function ()
    {
        //分享给朋友
        wx.onMenuShareAppMessage(shareData);

        //分享给朋友圈
        wx.onMenuShareTimeline(shareData);

        if (!pub_wx_hide_menu)
        {
            setTimeout(function ()
            {
                func_set_share_menu("show", ['menuItem:share:appMessage', 'menuItem:share:timeline']);
            }, 200);
        }
    });
}

//////////////////////////////
///获取分享参数
function func_init_shareData()
{
    var title = $('#wx__title').val();
    var desc = $('#wx__desc').val();
    var link = $('#wx__url').val();
    var imgUrl = $('#wx__img').val();

    var type = "";		// 分享类型,music、video或link，不填默认为link
    var dataUrl = "";	// 如果type是music或video，则要提供数据链接，默认为空


    var shareData =
        {
            title: title,
            desc: desc,
            link: link,
            imgUrl: imgUrl,

            type: type,
            dataUrl: dataUrl,

            trigger: function (res)
            {
                func_onMenuShare_trigger(res);
            },
            success: function (res)
            {
                func_onMenuShare_success(res);
            },
            cancel: function (res)
            {
                func_onMenuShare_cancel(res);
            },
            fail: function (res)
            {
                //alert(JSON.stringify(res));

                func_onMenuShare_fail(res);
            },
            complete: function (res)
            {
                func_onMenuShare_complete(res);
            }
        };


    return shareData;
}


function func_onMenuShare_trigger(res)
{
}

function func_onMenuShare_success(res)
{
}

function func_onMenuShare_cancel(res)
{
}

function func_onMenuShare_fail(res)
{
}

function func_onMenuShare_complete(res)
{
}


//////////////////////////////
///设置微信分享菜单，显示隐藏项目
function func_set_share_menu(type, menuList)
{
    if (menuList == "")
    {
        menuList = [
            'menuItem:share:appMessage',
            'menuItem:share:timeline',
            'menuItem:share:qq',
            'menuItem:share:weiboApp',
            'menuItem:share:QZone'
        ];
    }


    wx.ready(function ()
    {
        if (type == "show")
        {
            wx.showMenuItems({
                menuList: menuList,
                success: function (res)
                {
                },
                fail: function (res)
                {
                    alert(JSON.stringify(res));
                }
            });
        }
        else if (type == "hide")
        {
            wx.hideMenuItems({
                menuList: menuList,
                success: function (res)
                {
                },
                fail: function (res)
                {
                    alert(JSON.stringify(res));
                }
            });
        }
    });
}


console.log("weui:" + typeof (weui));
if (typeof (weui) != "undefined")
{
    var pub_loading;

    function func_loading(is_hide, content)
    {
        if (!content)
        {
            content = "加载中";
        }

        if (is_hide)
        {
            if (pub_loading)
            {
                pub_loading.hide(function ()
                {
                    console.log('加载框隐藏');
                });
            }
        }
        else
        {
            pub_loading = weui.loading(content, {
                className: 'custom-loading'
            });
        }
    }


    function func_alert(msg)
    {
        weui.alert(msg);
    }
}

//////////////////////////////
///获取头像url
function func_get_wx_headimgurl(url, size)
{
    if (!size)
    {
        size = 96;
    }

    if (url)
    {
        url = url.replace("/0", "/" + size);

        return url;
    }

    return "";
}


//////////////////////////////
///显示分享遮罩
function func_show_share_mask(url)
{
    func_init_share_mask();

    if (url)
    {
        $("#share_mask_img")[0].src = url;
    }
    else
    {
        //默认图片
        var common_url = "/x_res/img/show_share_mask.png";
        $("#share_mask_img").attr("src", common_url);
    }

    $("#share_mask").show();
}

//////////////////////////////
///初始化遮罩
function func_init_share_mask()
{
    if ($("#share_mask").length > 0)
    {
        console.log("share_mask 存在");
    }
    else
    {
        var div = '<div id="share_mask" style="display: none;">'
            + '<img id="share_mask_img" width="320" src="about:blank" />'
            + '</div>';

        $("body").prepend(div);


        $("#share_mask").click(function ()
        {
            $(this).hide();
        });
    }
}
